package com.example.designpatterns.command.receiver;

/**
 * Light类 - 命令的接收者，负责执行具体操作
 */
public class Light {
    private String location;
    private boolean isOn;
    private int brightness; // 亮度范围：0-100
    
    /**
     * 创建一个灯光对象
     * @param location 灯光位置
     */
    public Light(String location) {
        this.location = location;
        this.isOn = false;
        this.brightness = 0;
        System.out.println(location + "的灯光已初始化");
    }
    
    /**
     * 打开灯光
     */
    public void turnOn() {
        if (!isOn) {
            isOn = true;
            brightness = 50; // 默认亮度为中等
            System.out.println(location + "的灯光已打开，亮度为" + brightness);
        } else {
            System.out.println(location + "的灯光已经是打开状态");
        }
    }
    
    /**
     * 关闭灯光
     */
    public void turnOff() {
        if (isOn) {
            isOn = false;
            brightness = 0;
            System.out.println(location + "的灯光已关闭");
        } else {
            System.out.println(location + "的灯光已经是关闭状态");
        }
    }
    
    /**
     * 设置灯光亮度
     * @param level 亮度级别（0-100）
     */
    public void setBrightness(int level) {
        if (level < 0) {
            level = 0;
        } else if (level > 100) {
            level = 100;
        }
        
        // 如果亮度大于0，则自动打开灯光
        if (level > 0 && !isOn) {
            isOn = true;
        } else if (level == 0 && isOn) {
            isOn = false;
        }
        
        int oldBrightness = brightness;
        brightness = level;
        System.out.println(location + "的灯光亮度从" + oldBrightness + "调整到" + brightness);
    }
    
    /**
     * 判断灯是否开启
     * @return 灯光是否开启
     */
    public boolean isOn() {
        return isOn;
    }
    
    /**
     * 获取灯光亮度
     * @return 灯光亮度
     */
    public int getBrightness() {
        return brightness;
    }
    
    /**
     * 获取灯光位置
     * @return 灯光位置
     */
    public String getLocation() {
        return location;
    }
    
    /**
     * 获取灯光状态信息
     * @return 状态描述
     */
    public String getStatus() {
        return location + "的灯光状态：" + (isOn ? "开启" : "关闭") + 
               (isOn ? "，亮度为" + brightness : "");
    }
} 